package com.ruoyi.flow.common.tools.mediaconverter.converter.reflections.scanners;

import java.lang.reflect.Member;
import java.util.List;

import com.ruoyi.flow.common.tools.mediaconverter.converter.reflections.util.JavaReflectionUtils;

/** scans methods/constructors and indexes parameters, return type and parameter annotations */
@SuppressWarnings("unchecked")
public class MethodParameterScanner extends AbstractScanner {

    @Override
    public void scan(Class cls) {
        for (Member method : JavaReflectionUtils.getMethods(cls)) {

            String signature = JavaReflectionUtils.getParameterNames(method).toString();
            if (acceptResult(signature)) {
                getStore().put(signature, JavaReflectionUtils.getMethodFullKey(cls, method));
            }

            String returnTypeName = JavaReflectionUtils.getReturnTypeName(method);
            if (acceptResult(returnTypeName)) {
                getStore().put(returnTypeName, JavaReflectionUtils.getMethodFullKey(cls, method));
            }

            List<String> parameterNames = JavaReflectionUtils.getParameterNames(method);
            for (int i = 0; i < parameterNames.size(); i++) {
                for (Object paramAnnotation : JavaReflectionUtils.getParameterAnnotationNames(method, i)) {
                    if (acceptResult((String) paramAnnotation)) {
                        getStore().put((String) paramAnnotation, JavaReflectionUtils.getMethodFullKey(cls, method));
                    }
                }
            }
        }
    }
}
